package com.cloudstore.eccom.avic;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudstore.dev.api.bean.MessageBean;
import com.weaver.base.msgcenter.channel.IMessageReceive;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author dusongsong
 * @date 2023年09月19日 11:40
 */
public class WorkflowMessage extends BaseBean implements IMessageReceive {
    @Override
    public void onMessage(String paramString) {
        writeLog(">>>>>WorkflowMessage>>>>>paramString=" + paramString);
        List<MessageBean> localList = JSONArray.parseArray(paramString, MessageBean.class);
        try {
            ResourceComInfo comInfo = new ResourceComInfo();
            if (localList != null && !localList.isEmpty()) {
                for (MessageBean messageBean : localList) {
                    int code = messageBean.getMessageType().getCode();
                    String detailBaseName = Util.null2String(messageBean.getParams().get("detailBaseName"));
                    String operatorId = Util.null2String(messageBean.getParams().get("operatorId"));
                    String datetime = Util.null2String(messageBean.getParams().get("date"));
                    String receiveDate = Util.null2String(messageBean.getParams().get("receiveDate"));
                    String operaterDate = Util.null2String(messageBean.getParams().get("operaterDate"));
                    String creater = comInfo.getLastname(messageBean.getCreater() + "");
                    String requestid = messageBean.getTargetId();
                    String title = messageBean.getTitle();
                    Set<String> userList = messageBean.getUserList();
                    writeLog(">>>>>WorkflowMessage>>>>>getCode=" + code);
                    if (code == 19 || code == 20 || code == 21 || code == 22 || code == 23 || code == 24) {
                        for (String userid : userList) {
                            JSONObject nodebase = DoingWfUtil.getNodeBase(requestid);
                            String loginId = comInfo.getLoginID(userid);
                            String pcurl = "http://xtbg.wxgyxy.edu.cn/login/ssoTogyxy.jsp?loginid=" + loginId + "&requestid=" + requestid;
                            String emurl = "http://ydoa.wxgyxy.edu.cn:9000/login/ssoTogyxyEM.jsp?loginid=" + loginId + "&requestid=" + requestid;
                            writeLog(">>>>>WorkflowMessage>>>>>loginid=" + loginId + ",requestid=" + requestid);
                            JSONObject paramsJson = new JSONObject();
                            paramsJson.put("flowid", requestid);
                            paramsJson.put("requestname", title);
                            paramsJson.put("workflowname", detailBaseName);
                            paramsJson.put("nodename", nodebase.getString("nodename"));
                            paramsJson.put("pcurl", pcurl);
                            paramsJson.put("appurl", emurl);
                            paramsJson.put("creator", creater);
                            paramsJson.put("createdatetime", operaterDate);
                            paramsJson.put("receiver", userid);
                            paramsJson.put("receivedatetime", receiveDate);
                            JSONObject DoingResultJson = DoingWfUtil.sendDoingWorkflow(paramsJson);
                            writeLog(">>>>>WorkflowMessage>>>>>DoingResultJson=" + DoingResultJson);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onStart() {
        writeLog(">>>>>WorkflowMessage>>>>>onStart");
    }

    @Override
    public void onEnd() {
        writeLog(">>>>>WorkflowMessage>>>>>onEnd");
    }

    @Override
    public void setProp(Map<String, String> map) {

    }

    @Override
    public void setList(List<IMessageReceive> list) {

    }
}
